package com.bawei.composeui2207a.day04.mvi.login

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.bawei.composeui2207a.day04.mvi.base.OnError
import com.bawei.composeui2207a.day04.mvi.base.OnLoading
import com.bawei.composeui2207a.day04.mvi.base.OnSuccess
import com.bawei.composeui2207a.model.LoginEntity
import com.bawei.composeui2207a.model.RegisterEntity
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/13 17:08
 * 功能：
 */
@Composable
fun LoginPage(viewModel: LoginViewModel = hiltViewModel()) {

    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {}
                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        when (it.data) {
                            is LoginEntity -> {
                                ToastUtils.showShort(it.data.message)
                            }

                            is RegisterEntity -> {
                                ToastUtils.showShort(it.data.message)
                            }
                        }
                    }
                }
            }
        }
    }

    var phone by remember {
        mutableStateOf("")
    }
    var pwd by remember {
        mutableStateOf("")
    }

    Column(modifier = Modifier.padding(start = 10.dp, top = 24.dp, end = 10.dp)) {

        Text(text = "注册或登录", fontSize = 40.sp)
        OutlinedTextField(
            value = phone,
            onValueChange = { phone = it },
            modifier = Modifier.fillMaxWidth(),
            label = {
                Text(
                    text = "手机号"
                )
            },
            placeholder = { Text(text = "请输入手机号") })
        OutlinedTextField(
            value = pwd,
            onValueChange = { pwd = it },
            modifier = Modifier.fillMaxWidth(),
            label = {
                Text(
                    text = "密码"
                )
            },
            visualTransformation = PasswordVisualTransformation(),
            placeholder = { Text(text = "请输入密码") })

        Button(onClick = {
            // 发送注册意图
            viewModel.sendIntent(LoginIntent.Register(mapOf("phone" to phone, "pwd" to pwd)))
        }) {
            Text(text = "注册")
        }
        Button(onClick = {
            // 发送登录意图
            viewModel.sendIntent(LoginIntent.Login(mapOf("phone" to phone, "pwd" to pwd)))
        }) {
            Text(text = "登录")
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun LoginPagePreview() {
    LoginPage()
}

